/**
 * author      : dormi330
 * date        : 2018/6/26
 * project     : mybatis
 * description : 应用层服务
 */

package com.deep.trade.application;

import com.deep.trade.adaptor.required.member.Member;
import com.deep.trade.adaptor.required.member.MemberService;
import com.deep.trade.application.order.*;
import com.deep.trade.order.*;
import com.deep.trade.order.query.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderServiceFacade implements OrderApi, OrderQueryApi {

    @Autowired
    private PlaceOrderService placeOrderService;

    @Autowired
    private OrderQueryService orderQueryService;

    @Autowired
    private AuditOrderService auditOrderService;

    @Autowired
    private CancelOrderService cancelOrderService;

    @Autowired
    private PayOrderService payOrderService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private AddParcelService addParcelService;

    //========================================== OrderApi ==========================================//
    @Override
    public void placeOrder(InDtoPlaceOrder inDtoPlaceOrder) throws Exception {
        placeOrderService.placeOrder(inDtoPlaceOrder);
    }

    @Override
    public void auditTradeOrder(InDtoAuditOrder inDtoAuditOrder) throws Exception {
        auditOrderService.auditTradeOrder(inDtoAuditOrder);
    }


    @Override
    public void cancelTradeOrder(InDtoCancelOrder inDtoCancelOrder) throws Exception {
        cancelOrderService.cancelTradeOrder(inDtoCancelOrder);
    }

    @Override
    public void payOrderSummary(InDtoPayOrderSummary inDtoPayOrderSummary) throws Exception {
        payOrderService.payOrderSummary(inDtoPayOrderSummary);
    }

    @Override
    public void payTradeOrder(InDtoPayTradeOrder inDtoPayTradeOrder) throws Exception {
        payOrderService.payTradeOrder(inDtoPayTradeOrder);
    }

    @Override
    public void addParcel(List<InDtoEachParcel> inDtoEachParcelList) throws Exception {
        addParcelService.addParcel(inDtoEachParcelList);
    }

    //========================================== OrderQueryApi ==========================================//

    public BigDecimal calculatePayment(InDtoPaymentCalculate inDtoPaymentCalculate) throws Exception {
        return payOrderService.calculatePayment(inDtoPaymentCalculate);
    }


    @Override
    public PageInfo<OutDtoOrderSummary> orderSummaryOfMember(String tenantCode, String token, int pageNum, int pageSize) throws Exception {
        Member member = memberService.memberOfToken(tenantCode, token);
        return orderQueryService.orderSummaryOfMember(tenantCode, member.getId(), pageNum, pageSize);
    }

    /**
     * 获取用户的订单列表
     */
    @Override
    public PageInfo<OutDtoTradeOrder> tradeOrderListOfMember(String tenantCode, String token, int pageNum, int pageSize) throws Exception {
        Member member = memberService.memberOfToken(tenantCode, token);
        return orderQueryService.tradeOrderListOfMember(tenantCode, member.getId(), pageNum, pageSize);
    }

    /**
     * 获取用户的订单列表, 订单包含 sku信息
     */
    @Override
    public PageInfo<OutDtoTradeOrderWithSku> tradeOrderWithSkuListOfMember(String tenantCode, String token, int pageNum, int pageSize) throws Exception {
        Member member = memberService.memberOfToken(tenantCode, token);
        return orderQueryService.tradeOrderWithSkuListOfMember(tenantCode, member.getId(), pageNum, pageSize);
    }


    /**
     * 分页获取 某个租户对所有 订单
     */
    @Override
    public PageInfo<OutDtoTradeOrder> tradeOrderListOfTenant(String tenantCode, int pageNum, int pageSize) throws Exception {
        return orderQueryService.tradeOrderListOfTenant(tenantCode, pageNum, pageSize);
    }

    @Override
    public OutDtoOrderDetail tradeOrderDetail(String tenantCode, String tradeOrderId) throws Exception {
        return orderQueryService.tradeOrderDetail(tenantCode, tradeOrderId);
    }
    //========================================== OrderQueryApi ==========================================//

}
